package cn.tedu.thread;

//测试 多线程售票
public class Test1_Tickets2 {
    public static void main(String[] args) {
        MyTickets2 target = new MyTickets2();
        Thread t1 = new Thread(target);
        t1.start();
        Thread t2 = new Thread(target);
        t2.start();
        Thread t3 = new Thread(target);
        t3.start();
        Thread t4 = new Thread(target);
        t4.start();

    }
}
class MyTickets2 implements Runnable{
    int tickets = 100;

    @Override
    public void run() {
        while (true){
            synchronized (this) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "===" + tickets--);
                } else {
                    break;
                }
            }
        }
    }
}